<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/04/0004
 * Time: 13:15
 */

namespace app\sports_admin\controller;


use app\exception\StudentException;
use think\Request;
use app\sports_admin\model\Student as StudentModel;
class Student extends Base
{
    /**
     * 学生首页
     * $teacher_id 教师id
     * $school_id 学校id
    */
    public function index()
    {
        $teacherId = input('get.teacher_id');
        $schoolId = input('get.school_id');
        return view('index',compact('teacherId','schoolId'));
    }
    /**
     * 查看学生信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $schoolId 学校id
     * @param $teacherId 教师id
    */
    public function studentList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','schoolId','teacherId']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $schoolId = intval($params['schoolId']);
        $teacherId = intval($params['teacherId']);
        return StudentModel::getStudentList($page,$rows,$sort,$sortOrder,$searchName,$schoolId,$teacherId);
    }

    /**
     * 删除学生信息
     * @param $id 学生信息id
    */
    public function delete(Request $request)
    {
        if($request->isAjax()){
            $id = $request->post('id');
            $bool = StudentModel::destroy($id);
            if(!$bool){
                throw new StudentException([
                    'msg'=>'删除失败',
                    'errorCode'=>400
                ]);
            }
            return json(['code'=>200,'msg'=>'删除成功']);
        }
    }
}